package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.controller.dto.AfterSaleTask;
import cn.stylefeng.guns.modular.system.entity.AfterSale;
import cn.stylefeng.guns.modular.system.model.params.AfterSaleParam;
import cn.stylefeng.guns.modular.system.service.AfterSaleService;
import cn.stylefeng.guns.modular.system.service.ClassificationService;
import cn.stylefeng.guns.modular.system.service.ProductOrderService;
import cn.stylefeng.guns.modular.system.service.TroubleTypeService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.core.util.common.SystemConstant;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 控制器
 *
 * @author ZhangJun
 * @Date 2019-06-25 16:06:19
 */
@Controller
@RequestMapping("/afterSale")
public class AfterSaleController extends BaseController {

    private String PREFIX = "/afterSale";

    @Autowired
    private AfterSaleService afterSaleService;
    @Resource
    private TaskService taskService;
    @Autowired
    private ClassificationService classificationService;
    @Autowired
    private ProductOrderService productOrderService;
    @Autowired
    private TroubleTypeService troubleTypeService;
    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author ZhangJun
     * @Date 2019-06-25
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/afterSale.html";
    }

    /**
     * 新增页面
     *
     * @author ZhangJun
     * @Date 2019-06-25
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/afterSale_add.html";
    }

    /**
     * 编辑页面
     *
     * @author ZhangJun
     * @Date 2019-06-25
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/afterSale_edit.html";
    }

    /**
     * 新增接口
     *
     * @author ZhangJun
     * @Date 2019-06-25
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(AfterSaleParam afterSaleParam) {
        this.afterSaleService.add(afterSaleParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author ZhangJun
     * @Date 2019-06-25
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(AfterSaleParam afterSaleParam) {
        this.afterSaleService.update(afterSaleParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author ZhangJun
     * @Date 2019-06-25
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(AfterSaleParam afterSaleParam) {
        this.afterSaleService.delete(afterSaleParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author ZhangJun
     * @Date 2019-06-25
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(AfterSaleParam afterSaleParam) {
        AfterSale detail = this.afterSaleService.getById(afterSaleParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author ZhangJun
     * @Date 2019-06-25
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(AfterSaleParam afterSaleParam) {
        return this.afterSaleService.findPageBySpec(afterSaleParam);
    }

    /**
     * 跳转审核页面
     * @return
     */
    @RequestMapping("/listIndex")
    public String listIndex(){
        return  PREFIX+"/afterSale_check.html";
    }

    @ResponseBody
    @RequestMapping("/factoryList")
    public LayuiPageInfo factoryList(){

        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey(SystemConstant.charAfterSale).taskCandidateUser(ShiroKit.getUserNotNull().getId() +"").list();

        if (tasks.size() == 0){
            return  new LayuiPageInfo();
        }

       List<AfterSaleTask> afterSaleTasks = tasks.stream().map(task -> {
           AfterSaleTask afterSaleTask = new AfterSaleTask();
           AfterSale afterSale = afterSaleService.getOne(new QueryWrapper<AfterSale>()
           .lambda().eq(AfterSale::getProcessInstanceId,task.getProcessInstanceId()));
           BeanUtils.copyProperties(afterSale,afterSaleTask);
           Long id = productOrderService
                   .getById(afterSale.getTroubleProduct()).getKindId();
           Long pid = classificationService.getById(id).getPid();
           Long pidd = classificationService.getById(pid).getPid();
           afterSaleTask.setProductOrderName(classificationService.getById(id).getName());
           afterSaleTask.setProductModelName(classificationService.getById(pid).getName());
           afterSaleTask.setProductName(classificationService.getById(pidd).getName());
           afterSaleTask.setProcessInstanceId(task.getProcessInstanceId());
           afterSaleTask.setSalesUserName(userService.getById(afterSale.getSalesUserId()).getName());
           afterSaleTask.setTaskId(task.getId());
           afterSaleTask.setTroubleTypeStr(troubleTypeService.getById(afterSale.getTroubleTypeId()).getTroubleType());
           return  afterSaleTask;
       }).collect(Collectors.toList());

        LayuiPageInfo pageInfo = new LayuiPageInfo();
        pageInfo.setData(afterSaleTasks);
        pageInfo.setCount(afterSaleTasks.size());
        return  pageInfo;
    }



}


